package com.example.test.哈夫曼编码;

public class Node implements Comparable<Node> {

    Byte data;

    //节点权值
    int weight;

    //指向左子节点
    Node left;

    //指向右子节点
    Node right;

    public Node(Byte data, int weight) {
        this.data = data;
        this.weight = weight;
    }

    //前序遍历，先输出父节点，再遍历左子树和右子树
    public void preOrder(){
        System.out.println(this);
        if(null!=this.left){
            this.left.preOrder();
        }

        if(null!=this.right){
            this.right.preOrder();
        }

    }

    @Override
    public int compareTo(Node o) {
        return this.weight-o.weight;
    }

    @Override
    public String toString() {
        return "Node{" +
                "data=" + data +
                ", weight=" + weight +
                ", left=" + left +
                ", right=" + right +
                '}';
    }
}
